package com.behome.netty.netty_D.bean;

import com.behome.netty.common.ByteConventerKit;
import com.behome.netty.common.CapCmdManager;
import io.netty.util.internal.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/**
 * Created by cky on 2020/4/29.
 */
public class PacketBody {
    protected static Logger logger = LoggerFactory.getLogger(PacketBody.class);
    static final int RESOURCE_CODE_LENGTH = 12;
    private String sourceCode;
    private String[] targetCodes;
    private BusinessCmdBase businessCmd;

    public PacketBody() {
    }

    public PacketBody(String sourceCode, String[] targetCodes, BusinessCmdBase businessCmd) {
        this.sourceCode = sourceCode;
        this.targetCodes = targetCodes;
        this.businessCmd = businessCmd;
    }

    public byte[] serialize() {
        if (StringUtil.isNullOrEmpty(this.sourceCode)) {
            throw new NullPointerException("sourceCode不能为空");
        } else if (this.businessCmd == null) {
            throw new NullPointerException("businessCmd不能为空");
        } else {
            byte[] businessCmdData = this.businessCmd.serialize();
            List<byte[]> targerCodesData = new ArrayList<>();
            if (this.targetCodes == null || this.targetCodes.length == 0) {
                this.targetCodes = new String[]{"F00000000000000000000000"};
            }
            String[] var3 = this.targetCodes;
            for (String targetResourceCode : var3) {
                if (!StringUtil.isNullOrEmpty(targetResourceCode)) {
                    targerCodesData.add(ByteConventerKit.serializeResourceCode(targetResourceCode));
                }
            }

            int length = 14 + 12 * targerCodesData.size() + 1 + 2 + businessCmdData.length;
            ByteBuffer byteBuffer = ByteBuffer.allocate(length);
            byteBuffer.put(Objects.requireNonNull(ByteConventerKit.serializeResourceCode(this.sourceCode))).put(ByteConventerKit.shortToBytes(Integer.valueOf(targerCodesData.size()).shortValue()));
            if (!targerCodesData.isEmpty()) {
                for (byte[] codeData : targerCodesData) {
                    byteBuffer.put(codeData);
                }
            }

            //心跳 一般回复处理
            if (this.businessCmd.getCmdTypeValue() == 103) {
                byteBuffer.put((byte) 16).put(ByteConventerKit.shortToBytes(Integer.valueOf(businessCmdData.length).shortValue())).put(businessCmdData);
            } else {
                byteBuffer.put(this.businessCmd.getCmdTypeValue()).put(ByteConventerKit.shortToBytes(Integer.valueOf(businessCmdData.length).shortValue())).put(businessCmdData);
            }
            return byteBuffer.array();
        }
    }

    public static PacketBody deserialize(byte[] bodyData) {
        PacketBody ret = new PacketBody();
        if (bodyData != null || bodyData.length > 0) {
            ByteBuffer byteBuffer = ByteBuffer.wrap(bodyData);
            byte[] sourceCodeData = new byte[12];
            byteBuffer.get(sourceCodeData);
            String sourceCode = ByteConventerKit.deserializeResourceCode(sourceCodeData);
            ret.setSourceCode(sourceCode);
            short targetCount = byteBuffer.getShort();
            byte[] targetCodes = new byte[targetCount * 12];
            byteBuffer.get(targetCodes);
            ByteBuffer tmpBuffer = ByteBuffer.wrap(targetCodes);
            String[] targetCodeStrs = new String[targetCount];

            for (int i = 0; i < targetCount; ++i) {
                byte[] tmpB = new byte[12];
                tmpBuffer.get(tmpB);
                targetCodeStrs[i] = ByteConventerKit.deserializeResourceCode(tmpB);
            }

            ret.setTargetCodes(targetCodeStrs);
            byte busiCmdType = byteBuffer.get();
            short busiCmdLength = byteBuffer.getShort();
            byte[] busiCmdData = new byte[busiCmdLength];
            byteBuffer.get(busiCmdData);
            Class<BusinessCmdBase> cmdClass = CapCmdManager.getProtocolDClass(busiCmdType);
            if (cmdClass != null) {
                BusinessCmdBase busiCmdBase = null;

                try {
                    busiCmdBase = cmdClass.newInstance();
                } catch (InstantiationException | IllegalAccessException var15) {
                    logger.error("程序异常" + var15);
                }

                ret.setBusinessCmd(busiCmdBase.deserialize(busiCmdData));
            }
        }

        return ret;
    }

    public String getSourceCode() {
        return this.sourceCode;
    }

    public void setSourceCode(String sourceCode) {
        this.sourceCode = sourceCode;
    }

    public String[] getTargetCodes() {
        return this.targetCodes;
    }

    public void setTargetCodes(String[] targetCodes) {
        this.targetCodes = targetCodes;
    }

    public BusinessCmdBase getBusinessCmd() {
        return this.businessCmd;
    }

    public void setBusinessCmd(BusinessCmdBase businessCmd) {
        this.businessCmd = businessCmd;
    }
}
